/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaLog.java
 *
 * Created on 29/04/2012, 09:14:26
 */
package br.com.fbv.distribuidos.view;

import br.com.fbv.distribuidos.controller.DNSConnectionHandler;

/**
 *
 * @author Wagner
 */
public class JanelaLog extends javax.swing.JFrame {

	private static final long serialVersionUID = -849339034915359391L;
	private static JanelaLog instance;
	private static DNSConnectionHandler conexao;

	/** Creates new form JanelaLog */
	public JanelaLog() {
		initComponents();
		setLocationRelativeTo(null);
	}

	public static JanelaLog getInstance() {
		if(instance==null){
			JanelaLog.instance = new JanelaLog();
		}
		return instance;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		log_in = new javax.swing.JTextArea();
		ip_txt = new javax.swing.JLabel();
		ip_in = new javax.swing.JTextField();
		conectar_btn = new javax.swing.JButton();
		porta_txt = new javax.swing.JLabel();
		porta_in = new javax.swing.JTextField();
		identificar_btn = new javax.swing.JButton();
		log_txt = new javax.swing.JLabel();
		limpar_btn = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Processador"); // NOI18N
		setName("Form"); // NOI18N

		jScrollPane1.setName("jScrollPane1"); // NOI18N

		log_in.setColumns(10);
		log_in.setEditable(false);
		log_in.setLineWrap(true);
		log_in.setRows(5);
		log_in.setName("log_in"); // NOI18N
		jScrollPane1.setViewportView(log_in);

		ip_txt.setText("IP:"); // NOI18N
		ip_txt.setName("ip_txt"); // NOI18N

		ip_in.setName("ip_in"); // NOI18N
		ip_in.setText("192.168.0.102");

		conectar_btn.setText("Conectar"); // NOI18N
		conectar_btn.setName("conectar_btn"); // NOI18N
		conectar_btn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				conectar_btnActionPerformed(evt);
			}
		});

		porta_txt.setText("Porta:"); // NOI18N
		porta_txt.setName("porta_txt"); // NOI18N

		porta_in.setName("porta_in"); // NOI18N
		porta_in.setText("2004");

		identificar_btn.setText("Identificar-se"); // NOI18N
		identificar_btn.setName("identificar_btn"); // NOI18N
		identificar_btn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				identificar_btnActionPerformed(evt);
			}
		});

		log_txt.setText("Log:"); // NOI18N
		log_txt.setName("log_txt"); // NOI18N

		limpar_btn.setText("Limpar Log"); // NOI18N
		limpar_btn.setName("limpar_btn"); // NOI18N
		limpar_btn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				limpar_btnActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 721, Short.MAX_VALUE)
								.addGroup(layout.createSequentialGroup()
										.addComponent(ip_txt)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(ip_in, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(porta_txt)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(porta_in, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(conectar_btn)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(identificar_btn)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(limpar_btn, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
										.addComponent(log_txt))
										.addContainerGap())
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(ip_in, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(ip_txt)
								.addComponent(porta_in, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(porta_txt)
								.addComponent(conectar_btn)
								.addComponent(identificar_btn)
								.addComponent(limpar_btn))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(log_txt)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap())
				);

		pack();
	}// </editor-fold>

	private void conectar_btnActionPerformed(java.awt.event.ActionEvent evt) {                                             
		ip_in.setEnabled(false);
		porta_in.setEnabled(false);
		conectar_btn.setEnabled(false);
		conexao = new DNSConnectionHandler(ip_in.getText(), porta_in.getText());
		conexao.start();
	}                                            

	private void identificar_btnActionPerformed(java.awt.event.ActionEvent evt) {                                                
		conexao.identificar();
	}  

	private void limpar_btnActionPerformed(java.awt.event.ActionEvent evt) {
		log_in.setText("");
	}

	public void log(String msg) {
		log_in.setText(log_in.getText()+"\n"+ msg);
	}



	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
		/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(JanelaLog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(JanelaLog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(JanelaLog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(JanelaLog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new JanelaLog().setVisible(true);
			}
		});
	}
	// Variables declaration - do not modify
	private javax.swing.JButton conectar_btn;
	private javax.swing.JButton identificar_btn;
	private javax.swing.JTextField ip_in;
	private javax.swing.JLabel ip_txt;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea log_in;
	private javax.swing.JLabel log_txt;
	private javax.swing.JTextField porta_in;
	private javax.swing.JLabel porta_txt;
	private javax.swing.JButton limpar_btn;
	// End of variables declaration
}
